package com.flyco.modules.system.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.system.entity.SysMsgNotice;
import com.flyco.modules.system.model.StoreInfoModel;
import com.flyco.modules.system.service.ISysMsgNoticeService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 消息通知
 * @Author: flyco
 * @Date: 2024-06-19
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "消息通知")
@RestController
@RequestMapping("/f/system/sysMsgNotice")
public class SysMsgNoticeController extends BaseController<SysMsgNotice, ISysMsgNoticeService> {
    @Autowired
    private ISysMsgNoticeService sysMsgNoticeService;

    /**
     * 分页列表查询
     *
     * @param sysMsgNotice
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "消息通知-分页列表查询")
    @ApiOperation(value = "消息通知-分页列表查询", notes = "消息通知-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(SysMsgNotice sysMsgNotice,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<SysMsgNotice> queryWrapper = QueryGenerator.initQueryWrapper(sysMsgNotice, req.getParameterMap());
        Page<SysMsgNotice> page = new Page<SysMsgNotice>(pageNo, pageSize);
        queryWrapper.eq("delete_status", 0);
        queryWrapper.eq("msg_type", 0);
        IPage<SysMsgNotice> pageList = sysMsgNoticeService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param sysMsgNotice
     * @return
     */
    @AutoLog(value = "消息通知-添加")
    @ApiOperation(value = "消息通知-添加", notes = "消息通知-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SysMsgNotice sysMsgNotice) {
        sysMsgNoticeService.saveWithRecord(sysMsgNotice);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param sysMsgNotice
     * @return
     */
    @AutoLog(value = "消息通知-编辑")
    @ApiOperation(value = "消息通知-编辑", notes = "消息通知-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody SysMsgNotice sysMsgNotice) {
        sysMsgNoticeService.editWithRecord(sysMsgNotice);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "消息通知-通过id删除")
    @ApiOperation(value = "消息通知-通过id删除", notes = "消息通知-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) Long id) {
        sysMsgNoticeService.deleteWithRecord(id);
        return Result.ok("删除成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "消息通知-撤销审核")
    @ApiOperation(value = "消息通知-撤销审核", notes = "消息通知-撤销审核")
    @GetMapping(value = "/unAudit")
    public Result<?> unAudit(@RequestParam(name = "id", required = true) Long id) {
        sysMsgNoticeService.unAudit(id);
        return Result.ok("撤销审核成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "消息通知-批量删除")
    @ApiOperation(value = "消息通知-批量删除", notes = "消息通知-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.sysMsgNoticeService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "消息通知-通过id查询")
    @ApiOperation(value = "消息通知-通过id查询", notes = "消息通知-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) Long id) {
        SysMsgNotice sysMsgNotice = sysMsgNoticeService.getWithRecordById(id);
        return Result.ok(sysMsgNotice);
    }

    /**
     * 通过id查询
     *
     * @param channelIds
     * @return
     */
    @AutoLog(value = "消息通知-通过渠道查询经销商")
    @ApiOperation(value = "消息通知-通过渠道查询经销商", notes = "消息通知-通过渠道查询经销商")
    @GetMapping(value = "/queryStoreByChannel")
    public Result<?> queryStoreByChannel(@RequestParam(name = "channelIds", required = true) String channelIds) {
        List<StoreInfoModel> storeInfoList = sysMsgNoticeService.getStoreByChannel(channelIds);
        return Result.ok(storeInfoList);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param sysMsgNotice
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysMsgNotice sysMsgNotice) {
        return super.exportXls(request, sysMsgNotice, SysMsgNotice.class, "消息通知");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysMsgNotice.class);
    }

}
